home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 1_0-2 / EARTHPLO / EP_DOEAR.C < prev    next >
C/C++ Source or Header  |  1987-10-03  |  8KB  |  370 lines

  1. #include "ep.const.h"
  2. #include "ep.extern.h"
  3. #include "DialogMgr.h"
  4. #include "Math.h"
  5. #include "EventMgr.h"
  6.  
  7. #define pi100 0.03141592654    /* pi / 100 */
  8. #define fudge 10000
  9. /* real fudge so we can shift to divide rather than use expensive ldiv */
  10. #define fudge2 20078
  11.  
  12.  
  13. /*double        coslat, coslon, sinlon, x, y, z;*/
  14. double            coslat,alat, alon,coslon,sinlon,x,y,z,a_real;
  15. long        ix, iy, iz;
  16. double        alatd, alond, height; 
  17. double        cos1, cos2, sin1, sin2, xpos, xmax, scaler;
  18. long        icos1, icos2, isin1, isin2, ixmax, ixpos, iscaler;
  19. int            lat, lon, alt;
  20. int            ihalf_xsize,ihalf_ysize;
  21. char        s[255];
  22. extern        long    oldBackground,oldFore;
  23.  
  24. eraseTerra ()
  25. {
  26.     SetPort(eWindow);
  27.  
  28.     FillRect(&eWindow->portRect,white);
  29.     FrameOval(&eWindow->portRect);
  30. }
  31.  
  32. DrawEarth ()
  33. {
  34. register    int                    j,a,b,c;
  35. register    long                la,lb,lc;
  36.             int                    temp,i;
  37. register    unsigned char        **earthData;
  38. register    unsigned char        *ptr;
  39. EventRecord        theevent;
  40.  
  41.     SetPort(cWindow);
  42.     HiliteWindow(cWindow,FALSE);
  43.  
  44.     SetPort(eWindow);
  45.     BringToFront(eWindow);
  46.     HiliteWindow(eWindow,TRUE);
  47.  
  48.     SetCursor(*watchCursorHand);
  49.  
  50.     xsize = ixsize = eWindow->portRect.right  - eWindow->portRect.left - 2;
  51.     ysize = iysize = eWindow->portRect.bottom - eWindow->portRect.top  - 2;
  52.     half_xsize = ihalf_xsize = xsize / 2.0;
  53.     half_ysize = ihalf_ysize = ysize / 2.0;
  54.  
  55.     ClipRect(&eWindow->portRect);
  56.  
  57.     if (ePict != ebmPict)
  58.         HPurge(ePict);
  59.     HPurge(ebmPict);
  60.  
  61.     BackColor(backgroundC);
  62.     EraseRect(&eWindow->portRect);
  63.  
  64.     if (npictButton==1) {
  65.         ePict = OpenPicture(&eWindow->portRect);
  66.         ShowPen();
  67.     }
  68.  
  69.     ForeColor(earthbackgroundC);
  70.     PaintOval(&eWindow->portRect);
  71.  
  72.     ForeColor(earthoutlineC);
  73.     FrameOval(&eWindow->portRect);
  74.  
  75.     alatd = GetCtlValue(latSB);
  76.     alond = GetCtlValue(lonSB);
  77.     height= GetCtlValue(altSB)*(long)altScale;
  78.     if (GetCtlValue(mileCheck) == 0)
  79.         height = height * 1.61;
  80.  
  81.     alat = alatd * conv;
  82.     alon = alond * conv;
  83.     
  84.     cos1 = cos(alat);
  85.     cos2 = cos(alon);
  86.     sin1 = sin(alat);
  87.     sin2 = sin(alon);
  88.     
  89.     if (GetCtlValue(southCheck) == 1) sin1 = -sin1;
  90.     if (GetCtlValue(westCheck)  == 1) sin2 = -sin2;
  91.  
  92.     xpos = (height + diam) / diam;
  93.     xmax = 1.0 / xpos;
  94.     scaler = sqrt (1.0 - (xmax * xmax));
  95.  
  96.     icos1 = (long)(cos1 * fudge);
  97.     icos2 = (long)(cos2 * fudge);
  98.     isin1 = (long)(sin1 * fudge);
  99.     isin2 = (long)(sin2 * fudge);
  100.     ixmax = (long)(xmax * fudge);
  101.     ixpos = (long)(xpos * fudge);
  102.     iscaler = (long)(scaler * fudge);
  103.  
  104.     MoveTo((int)xsize,(int)half_ysize);
  105.     
  106.     draw = TRUE;
  107.     
  108.   if (nlnlBox) 
  109.         for (j = 1; j < 11; j++) {
  110.  
  111.             ForeColor(latC);
  112.  
  113.             alat   = (double)((j - 6) * 15) * conv;
  114.             coslat = cos(alat) * fudge;
  115.             iz = (long)(sin(alat) * fudge);
  116.             draw = FALSE;
  117.             over = FALSE;
  118.             for (i = 0; i < 204; i += 2) {
  119.  
  120.                 alon = (double)(i) * pi100;
  121.                 ix = (long)(coslat * cos(alon));
  122.                 iy = (long)(coslat * sin(alon));
  123.  
  124.                 xxx_int(ix, iy, iz);
  125.  
  126.                 draw = TRUE;
  127.  
  128.             }
  129.             if (GetNextEvent(everyEvent,&theevent) != 0)
  130.                 if (theevent.what == mouseDown) goto abort_drawing;
  131.         }
  132.  
  133.   if (nlnlBox)
  134.         for (j = 0; j < 24; j++) {
  135.  
  136.             ForeColor(latC);
  137.  
  138.             alon   = (double)((j * 15)) * conv;
  139.             coslon = cos(alon) * fudge;
  140.             sinlon = sin(alon) * fudge;
  141.             draw = FALSE;
  142.             over = FALSE;
  143.             for (i = 0; i < 104; i += 2) {
  144.  
  145.                 alat = ((double)(i) / 100.0 - 0.5) * pi;
  146.                 coslat = cos(alat);
  147.                 ix = (long)(coslat * coslon);
  148.                 iy = (long)(coslat * sinlon);
  149.                 iz = (long)(sin(alat) * fudge);
  150.  
  151.                 xxx_int(ix, iy, iz);
  152.  
  153.                 draw = TRUE;
  154.  
  155.             }
  156.             if (GetNextEvent(everyEvent,&theevent) != 0)
  157.                 if (theevent.what == mouseDown) goto abort_drawing;
  158.         }
  159.  
  160.      earthData = (unsigned char **)GetResource('eDAT',100);
  161.      HLock(earthData);
  162.      ptr = *earthData;
  163.     j = 0;
  164.     
  165.     ForeColor(landC);
  166.  
  167.     if (nfpButton) 
  168.         while (j</*9262*/27786) {
  169.             a = ptr[j++];
  170.             b = ptr[j++];
  171.             c = ptr[j++];
  172.             draw = (a+b+c) != 0;
  173.             if (!draw) {
  174.                 a = ptr[j++];
  175.                 b = ptr[j++];
  176.                 c = ptr[j++];
  177.             }
  178.             
  179.             x = (a / 127.5) - 1.0;
  180.             y = (b / 127.5) - 1.0;
  181.             z = (c / 127.5) - 1.0;
  182.             
  183.             xxx(x,y,z);
  184.                             
  185.             if (GetNextEvent(everyEvent,&theevent) != 0)
  186.                 if (theevent.what == mouseDown) goto abort_and_close;
  187.         }
  188.  
  189.     if (nintButton) 
  190.         while (j</*9262*/27786) {
  191.             a = ptr[j++];
  192.             b = ptr[j++];
  193.             c = ptr[j++];
  194.             draw = a || b || c;
  195.             if (!draw) {
  196.                 a = ptr[j++];
  197.                 b = ptr[j++];
  198.                 c = ptr[j++];
  199.             }
  200.             
  201.             la = (((long)a * fudge2) >> 8) - fudge;
  202.             lb = (((long)b * fudge2) >> 8) - fudge;
  203.             lc = (((long)c * fudge2) >> 8) - fudge;
  204.             
  205.             xxx_int(la,lb,lc);
  206.             
  207.             if (GetNextEvent(everyEvent,&theevent) != 0)
  208.                 if (theevent.what == mouseDown) goto abort_and_close;
  209.         }
  210.  
  211. abort_and_close:
  212.  
  213.  
  214. abort_drawing:    /******** !ARG! **********/
  215.  
  216.      HUnlock(earthData);
  217.  
  218.     if (npictButton==1) {
  219.         HidePen();
  220.         ClosePicture();
  221.     }
  222.         
  223.     ebmPict = OpenPicture(&eWindow->portRect);
  224.  
  225.     BackColor(backgroundC);
  226.     ForeColor(backgroundC == blackColor ? whiteColor : blackColor);
  227.  
  228.     if (FALSE /*hasColorQD*/) {
  229.         /*copy the pixMap, when I learn how (no MAC II around here!)*/
  230.     } else {
  231.         CopyBits(&eWindow->portBits,&eWindow->portBits,
  232.          &eWindow->portRect, &eWindow->portRect, srcCopy, 0L);
  233.     }
  234.  
  235.     ClosePicture();
  236.  
  237.     if (nbitmapButton==1)
  238.         ePict = ebmPict;
  239.  
  240.     InitCursor();
  241.  
  242.     oldBackground = backgroundC;
  243.     oldFore = backgroundC == blackColor ? whiteColor : blackColor;
  244.     myDrawGrowIcon();
  245. }
  246.  
  247. xxx(xabs,yabs,zabs)    /* plot a line <-- Richard's Name! */
  248. double    xabs,yabs,zabs;
  249. {
  250. register int        move;
  251. register int        x,y;
  252. static    int over = FALSE;
  253. double    factor,xtmp,xrel,yrel,zrel;
  254.  
  255.     move = ( ! draw) || over;
  256.     draw = ! move;
  257.     xtmp = xabs * cos2 + yabs * sin2;
  258.     xrel = xtmp * cos1 + zabs * sin1;
  259.     over = (xrel < xmax);
  260. /*    if (nhiddenlinesBox == 0) over = FALSE;    */
  261.  
  262.     if (over) return;
  263.  
  264.     yrel = yabs * cos2 - xabs * sin2;
  265.     zrel = zabs * cos1 - xtmp * sin1;
  266.     
  267.     factor = (-xpos) / (xrel - xpos);
  268.     
  269.     x = ((( yrel * factor * scaler) + 1.0) * half_xsize) + 1 ;
  270.     y = (((-zrel * factor * scaler) + 1.0) * half_ysize) + 1 ;
  271.     
  272.     if (move) MoveTo (x,y);
  273.     if (draw) LineTo (x,y);
  274. }
  275.  
  276. xxx_int(xabs, yabs, zabs)    /* plot a line or move current position */
  277. long    xabs, yabs, zabs;
  278. {
  279. register    int        x, y;
  280. register    long    ixtmp, ixrel, ifactor, iyrel, izrel;
  281.  
  282.     /*
  283.      * draw line if we want to draw (as opposed to move)
  284.      * and the previous point was drawn too.
  285.      */
  286.     draw = draw && !over;
  287.  
  288. /*    ixtmp = ((xabs * icos2) + (yabs * isin2)) / fudge;
  289.     ixrel = ((ixtmp * icos1) + (zabs * isin1)) / fudge;
  290. asm for SPEED! */
  291. asm {
  292.         move.l    xabs,d0        /*         xabs * icos2        */
  293.         move.l    icos2,d2
  294.         muls.w    d2,d0
  295.         move.l    yabs,d1        /*        yabs * isin2        */
  296.         move.l    isin2,d2
  297.         muls.w    d2,d1
  298.         add.l    d1,d0        /*        add these            */
  299.         divs.w    #fudge,d0    /*        / fudge                */
  300.         ext.l    d0
  301.         move.l    d0,ixtmp
  302.  
  303.     /*    move.l    xabs,d0                not required        */
  304.         move.l    icos1,d2    /*         ixtmp * icos1        */
  305.         muls.w    d2,d0
  306.         move.l    zabs,d1        /*        zabs * isin1        */
  307.         move.l    isin1,d2
  308.         muls.w    d2,d1
  309.         add.l    d1,d0        /*        add these            */
  310.         divs.w    #fudge,d0    /*        / fudge                */
  311.         ext.l    d0
  312.         move.l    d0,ixrel
  313.     }
  314.  
  315.     over = (ixrel < ixmax);
  316.  
  317.     if (over) return;
  318.  
  319. /*    iyrel = ((yabs * icos2) - (xabs * isin2)) / fudge;
  320.     izrel = ((zabs * icos1) - (ixtmp * isin1)) / fudge;
  321. asm for SPEED! */
  322. asm {
  323.         move.l    yabs,d0        /*         yabs * icos2        */
  324.         move.l    icos2,d2
  325.         muls.w    d2,d0
  326.         move.l    xabs,d1        /*        xabs * isin2        */
  327.         move.l    isin2,d2
  328.         muls.w    d2,d1
  329.         sub.l    d1,d0        /*        add these            */
  330.         divs.w    #fudge,d0    /*        / fudge                */
  331.         ext.l    d0
  332.         move.l    d0,iyrel
  333.  
  334.         move.l    zabs,d0        /*         zabs * icos1        */
  335.         move.l    icos1,d2    
  336.         muls.w    d2,d0
  337.         move.l    ixtmp,d1    /*        ixtmp * isin1        */
  338.         move.l    isin1,d2
  339.         muls.w    d2,d1
  340.         sub.l    d1,d0        /*        add these            */
  341.         divs.w    #fudge,d0    /*        / fudge                */
  342.         ext.l    d0
  343.         move.l    d0,izrel
  344.     }
  345.  
  346.     ifactor = ixpos * iscaler / (ixpos - ixrel);
  347.  
  348.     x = (((( iyrel * ifactor) / fudge) + fudge) * ihalf_xsize) / fudge + 1;
  349.       y = ((((-izrel * ifactor) / fudge) + fudge) * ihalf_ysize) / fudge + 1;
  350.  
  351.     if (draw)    LineTo (x, y);
  352.     else        MoveTo (x, y);
  353. }
  354.  
  355. DoEarthMenu (item)
  356. int        item;
  357. {
  358.     switch (item) {
  359.     case 1:
  360.         DrawEarth();
  361.         break;
  362.     case 2:
  363.         setOptions();
  364.          break;
  365.     case 3:
  366.         setColors();
  367.         break;
  368.     }
  369. }
  370.